package com.ruoyi.flow.flow.form;

import com.ruoyi.flow.flow.form.FormInfo;
import com.ruoyi.flow.flow.workflow.FlowService;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by fenghuo on 2018/8/28.
 */
@Service
public class FlowFormService {

    /**
     * 获取流程开始事件的表单信息
     *
     * @param defintionKey
     * @return
     */
    public com.ruoyi.flow.flow.form.FormInfo getStartProcessForm(String defintionKey) {
        ProcessDefinition definition = FlowService.definitionService().getProcessDefinitionByDefinitionKey(defintionKey);
        com.ruoyi.flow.flow.form.FormInfo formInfo = new com.ruoyi.flow.flow.form.FormInfo(definition);
        return formInfo;
    }

    /**
     * 获取任务节点的表单信息
     *
     * @param taskId
     * @return
     */
    public com.ruoyi.flow.flow.form.FormInfo getTaskProcessForm(String taskId) {
        TaskEntity taskEntity = FlowService.taskService().getTaskByTaskId(taskId);
        com.ruoyi.flow.flow.form.FormInfo formInfo = new FormInfo(taskEntity);
        return formInfo;
    }

    /**
     * 获取流程定义的发起表单或者任务的表单的表单内容
     *
     * @param instanceOrTaskId 流程实例ID或者任务ID。
     *                         <br>
     *                         根据流程定义ID获取发起表单，根据任务ID获取任务办理表单
     * @param startOrTask      如果获取发起表单，值为{@code start}；如果获取任务，参数为{@code task}
     * @return 如果没有表单内容，返回空字符串
     */
    public Map<String, Object> getFormData(String instanceOrTaskId, String startOrTask) {
        if (StringUtils.equals(startOrTask, "start")) {
            return FlowService.instanceService().getStartVariables(instanceOrTaskId);
        } else {
            return FlowService.taskService().getTaskVariables(instanceOrTaskId);
        }
    }


}
